/*
    OpenSys by Alexander Ljubizki
    Started 12.08.2013
    For more Info read the ChangeLog.txt
*/

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

/* Interne */
#include "Graphic/Graphic.h"
#include "Graphic/Input.h"
#include "Entity/Entity.h"
#include "World/World.h"
#include "Menu/Menu.h"

/* Funktionen */
static void Quit(int rc);

/* Hauptfunktion */
int main(int argc, char *argv[])
{
    int Runnig;
    SDL_TimerID Timer;

    /* Zufall */
    srand(time(0));

    /* Log einstellen */
    logInit();

    /* Graphic starten */
    if (!Graphic_Init()) {
        logError("Graphics can't be initialized!");
        return -1;
    }

    /* Input */
    Input_Init();

    /* World erstellen */
    World_Init(64);

    /* Entity start */
    Entity_Init();

    /* Menü in den RAM */
    Menu_Init();

    /* Programm startet */
    Runnig = 1;

    /* Timer */
    Timer = SDL_AddTimer (50, Entity_Process, NULL);

    Entity_Create( X2d(30,35), Y2d(30,35), 100, 1);
    Entity_Create( X2d(30,34), Y2d(30,34), 100, 2);

    /* Hauptschleife */
    while(Runnig)
    {
        /* World */
        World_Process();

        /* Entity */
        Entity_Process_Graphic();

        /* Menu */
        Menu_Process();

        Input_Process();

        /* Grafik darstellen */
        if(!Graphic_Process())
            Runnig = 0;
    }

    /* Graphic beenden */
    Graphic_Quit();

    /* Programm beenden */
    SDL_RemoveTimer( Timer);
    Quit(0);

    return 0;
}

/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void Quit(int rc)
{
    exit(rc);
}
